package edu.towson.cis.cosc490.lab6;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class ToDoItemDetailsActivity extends Activity implements OnClickListener {
	
	private Button m_addButton;
	private EditText m_editText;
	private RadioButton m_highPriority;
	private RadioButton m_mediumPriority;
	private RadioButton m_lowPriority;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details);
        initLayout();  
    }
    
    protected void initLayout(){
    	m_addButton = (Button) this.findViewById(R.id.addButton);
    	m_highPriority = (RadioButton) this.findViewById(R.id.highRadio);
    	m_mediumPriority = (RadioButton) this.findViewById(R.id.mediumRadio);
    	m_lowPriority = (RadioButton) this.findViewById(R.id.lowRadio);
    	
    	m_addButton.setOnClickListener(this);
        m_editText = (EditText) this.findViewById(R.id.editText1);
    }

	@Override
	public void onClick(View v) {
		if (m_editText.getText().toString().isEmpty()){
			Context context = getApplicationContext();
			CharSequence text = "To-Do Item Description is missing!";
			int duration = Toast.LENGTH_SHORT;

			Toast toast = Toast.makeText(context, text, duration);
			toast.show();
		}
		else if (!m_highPriority.isChecked() && !m_mediumPriority.isChecked() && !m_lowPriority.isChecked()){
			Context context = getApplicationContext();
			CharSequence text = "To-Do Item Priority is missing!";
			int duration = Toast.LENGTH_SHORT;

			Toast toast = Toast.makeText(context, text, duration);
			toast.show();
		}
		else {
			String newItem = m_editText.getText().toString();
			
			Intent result = new Intent();
			result.putExtra("newItem", newItem);  
			
			if(m_highPriority.isChecked())
				result.putExtra("priority", ToDoItem.high);
			else if(m_mediumPriority.isChecked())
				result.putExtra("priority", ToDoItem.medium);
			else if(m_lowPriority.isChecked())
				result.putExtra("priority", ToDoItem.low);
	      
			setResult(RESULT_OK, result);
	        finish();
		}
	}    
}
	